package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.databinding.FragmentPayPassBinding;
import com.whmnx.my.ui.fragment.SetPayWordFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/11/4 0004 13:53
 * version: 1.0
 */
public class LoginPayModel extends ToolbarViewModel{

    public ObservableField<String> phone = new ObservableField<>("");


    public ObservableField<String> code = new ObservableField<>("");

    SetPayWordFragment mFragment;
    public LoginPayModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(SetPayWordFragment setPayWordFragment, FragmentPayPassBinding binding) {
        this.mFragment = setPayWordFragment;
        setTitleText("重置交易密码");
    }


    //下一步
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (TextUtils.isEmpty(phone.get())){
                return;
            }
            if (TextUtils.isEmpty(code.get())){
                return;
            }

            if (!phone.get().equals(code.get())){
                ToastUtil.toastShortMessage("2次输入密码不一致");
                return;
            }

            Map<String, Object> params = new HashMap<>();
            params.put("Mobile", SPUtils.getInstance().getString(Constants.PHONE));
            params.put("Pwd", UItils.md5Digest(code.get()));
            params.put("type","1");
            HttpClient.get(mFragment.getActivity(), Constants.UPDATEPWD, params,new CallBack<LoginBean>() {
                @Override
                public void onSuccess(LoginBean result) {
                    SPUtils.getInstance().put(Constants.LV, UItils.md5Digest(code.get()));
                    finish();
                }
            });
        }
    });
}
